package com.jackrain.nea.fc.dl.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Mapper
public interface ViewDLTranToF4Mapper {

    @SelectProvider(type = ViewDLTranSql.class, method = "findtrandata")
    List<HashMap> selTranData(@Param("id") Object[] id,
                              @Param("status") Integer status,
                              @Param("isactive") String isactive,
                              @Param("sendStatus") Integer sendStatus,
                              @Param("changeStatus") Integer changeStatus);

    @SelectProvider(type = ViewDLTranSql.class, method = "findTranItemdata")
    List<HashMap> selTranItemData(@Param("id") Long id);

    @SelectProvider(type = ViewDLTranSql.class, method = "findTranSource")
    List<Long> selTranSourceData(@Param("id") Long id);

    @Select("SELECT ECODE FROM DL_C_BILLDIM_ITEM WHERE ID=#{addTypeId}")
    String selAddType(Long addTypeId);

    class ViewDLTranSql {

        public String findtrandata(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            Integer status = (Integer) map.get("status");
            String isactive = (String) map.get("isactive");
            Integer sendStatus = (Integer) map.get("sendStatus");
            Integer changeStatus = (Integer) map.get("changeStatus");
            return new SQL() {
                {
                    SELECT("ID,BILL_NO,BILL_DATE,CP_C_ORIG_ECODE,CP_C_DEST_ECODE,FROM_BILL_NO,MODIFY_BILL_NO," +
                            "OWNERENAME,CREATIONDATE,DATE_OUT,DATE_IN,TRAN_TYPE,ORIG_TYPE,REMARK,TRAN_FLAG,EMP_ENAME_OUT," +
                            "ARRIVAL_TIME,SP_STOCK_DATE,CYCLE_DAYS,CYCLE_DAYS_RET,EMP_ENAME_IN,ADD_TYPE_ID,MODIFIERNAME," +
                            "MODIFIERENAME,MODIFIEDDATE,ARRIVAL_TIME_APPLY,STATUSTIME,SEND_TIME,STORE_DATE_IN,STORE_DATE," +
                            "DEL_TIME,ACCEPT_TIME,CANCELER_TIME,IS_PICK");
                    FROM("DL_B_TRAN");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE("ID IN (" + sb.toString() + ")");
                    WHERE("IS_ADJUST <> 2");
                    if (null != status) {
                        WHERE("STATUS = " + status);
                    }
                    if (null != isactive) {
                        WHERE("ISACTIVE = '" + isactive + "'");
                    }
                    if (null != sendStatus) {
                        WHERE("SEND_STATUS = " + sendStatus);
                    }
                    if (null != changeStatus) {
                        WHERE("ACCEPTE_STATUS = " + changeStatus);
                    }
                }
            }.toString();
        }



        public String findTranItemdata(Map<String, Object> map) {
            Long id = (Long) map.get("id");
            return new SQL() {
                {
                    SELECT("PS_C_SKU_ECODE,QTY_BILL,QTY_OUT,QTY_IN,PRICE_LIST,AMT_BILL,AMT_OUT,AMT_IN," +
                            "IS_BF_SUPPLY,PRICE_COST,REMARK,SETTLEMENT_PRICE,SETTLEMENT_AMT");
                    FROM("DL_B_TRAN_ITEM_SKU");
                    WHERE("DL_B_TRAN_ID =" + id);
                }
            }.toString();
        }

        public String findTranSource(Map<String, Object> map) {
            Long id = (Long) map.get("id");
            return new SQL() {
                {
                    SELECT("DL_B_TRAN_ID_SOURCE");
                    FROM("DL_B_TRAN_SOURCE");
                    WHERE("DL_B_TRAN_ID_PICK =" + id);
                }
            }.toString();
        }

    }


}
